﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using XDerive.XStudio.Common.CommonAppI;
using XDerive.XStudio.SystemFramework;
using WebStudio.Mvc.Core.Helper;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// 普通参数扩展函数（XHtmlhelper）
    /// </summary>
    public static class ParameterExtension
    {
        #region 下拉框控件
        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, null);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, null, null, selectedValue, null);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="searchCondition"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, string searchCondition)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, null, null, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, object htmlAttributes)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, null, null, selectedValue, null, htmlAttributes);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="lableName"></param>
        /// <param name="lableValue"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, lableName, lableValue, selectedValue, null);
        }
        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="lableName"></param>
        /// <param name="lableValue">标签值</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="htmlAttributes">过滤条件</param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue, object htmlAttributes)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, lableName, lableValue, selectedValue, null, htmlAttributes);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="lableName">标签名称</param>
        /// <param name="lableValue">标签值</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件</param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue, string searchCondition)
        {
            return ParameterDropDownList(xHelper, name, parameterTypeCodeID, lableName, lableValue, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 普通参数下拉框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="lableName">标签名称</param>
        /// <param name="lableValue">标签值</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件，如：ParameterCodeID > 9</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterDropDownList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue, string searchCondition, object htmlAttributes)
        {
            List<SelectListItem> param = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(lableName))
            {
                param.Add(new SelectListItem() { Text = lableName, Value = lableValue, Selected = (selectedValue == null || selectedValue == lableValue) });
            }

            //条件
            string resultCondition = string.Format("ParameterTypeCodeID = {0}", parameterTypeCodeID);
            if (!string.IsNullOrEmpty(searchCondition))
            {
                resultCondition = string.Format("{0} AND ({1})", resultCondition, searchCondition);
            }

            using (IDataReader dr = ParameterAppI.GetParameterDataReader(resultCondition))
            {
                while (dr.Read())
                {
                    param.Add(new SelectListItem() { Text = SysFunction.StringParse(dr["ParameterName"]), Value = SysFunction.StringParse(dr["ParameterCodeID"]), Selected = (selectedValue != null && selectedValue == SysFunction.StringParse(dr["ParameterCodeID"])) });
                }
            }

            return xHelper.Html.DropDownList(name, param, htmlAttributes);
        }
        #endregion

        #region 列表框控件
        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, null);
        }

        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, null, null, selectedValue, null);
        }

        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="searchCondition"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, string searchCondition)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, null, null, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, object htmlAttributes)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, null, null, selectedValue, null, htmlAttributes);
        }

        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="lableName"></param>
        /// <param name="lableValue"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, lableName, lableValue, selectedValue, null);
        }


        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="lableName">标签名称</param>
        /// <param name="lableValue">标签值</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件</param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue, string searchCondition)
        {
            return ParameterListBox(xHelper, name, parameterTypeCodeID, lableName, lableValue, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 普通参数列表框控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="lableName">标签名称</param>
        /// <param name="lableValue">标签值</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件，如：ParameterCodeID > 9</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterListBox(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string lableName, string lableValue, string selectedValue, string searchCondition, object htmlAttributes)
        {
            List<SelectListItem> param = new List<SelectListItem>();
            if (!string.IsNullOrEmpty(lableName))
            {
                param.Add(new SelectListItem() { Text = lableName, Value = lableValue, Selected = (selectedValue == null || selectedValue == lableValue) });
            }

            //条件
            string resultCondition = string.Format("ParameterTypeCodeID = {0}", parameterTypeCodeID);
            if (!string.IsNullOrEmpty(searchCondition))
            {
                resultCondition = string.Format("{0} AND ({1})", resultCondition, searchCondition);
            }

            using (IDataReader dr = ParameterAppI.GetParameterDataReader(resultCondition))
            {
                while (dr.Read())
                {
                    param.Add(new SelectListItem() { Text = SysFunction.StringParse(dr["ParameterName"]), Value = SysFunction.StringParse(dr["ParameterCodeID"]), Selected = (selectedValue != null && selectedValue == SysFunction.StringParse(dr["ParameterCodeID"])) });
                }
            }

            return xHelper.Html.ListBox(name, param, htmlAttributes);
        }
        #endregion

        #region 单选框列表控件
        /// <summary>
        /// 普通参数单选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterRadioButtonList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID)
        {
            return ParameterRadioButtonList(xHelper, name, parameterTypeCodeID, null);
        }

        /// <summary>
        /// 普通参数单选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterRadioButtonList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue)
        {
            return ParameterRadioButtonList(xHelper, name, parameterTypeCodeID, selectedValue, null);
        }

        /// <summary>
        /// 普通参数单选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="searchCondition"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterRadioButtonList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, string searchCondition)
        {
            return ParameterRadioButtonList(xHelper, name, parameterTypeCodeID, selectedValue, searchCondition, null);
        }

        /// <summary>
        /// 普通参数单选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValue"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterRadioButtonList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, object htmlAttributes)
        {
            return ParameterRadioButtonList(xHelper, name, parameterTypeCodeID, selectedValue, null, htmlAttributes);
        }


        /// <summary>
        /// 普通参数单选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="selectedValue">选中值</param>
        /// <param name="searchCondition">过滤条件，如：ParameterCodeID > 9</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterRadioButtonList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, string selectedValue, string searchCondition, object htmlAttributes)
        {
            List<SelectListItem> param = new List<SelectListItem>();

            //条件
            string resultCondition = string.Format("ParameterTypeCodeID = {0}", parameterTypeCodeID);
            if (!string.IsNullOrEmpty(searchCondition))
            {
                resultCondition = string.Format("{0} AND ({1})", resultCondition, searchCondition);
            }

            using (IDataReader dr = ParameterAppI.GetParameterDataReader(resultCondition))
            {
                while (dr.Read())
                {
                    param.Add(new SelectListItem() { Text = SysFunction.StringParse(dr["ParameterName"]), Value = SysFunction.StringParse(dr["ParameterCodeID"]), Selected = (selectedValue != null && selectedValue == SysFunction.StringParse(dr["ParameterCodeID"])) });
                }
            }

            return xHelper.Html.RadioButtonList(name, param, htmlAttributes);
        }
        #endregion

        #region 多选框列表控件
        /// <summary>
        /// 普通参数多选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterCheckBoxList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID)
        {
            return ParameterCheckBoxList(xHelper, name, parameterTypeCodeID, null);
        }

        /// <summary>
        /// 普通参数多选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValues"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterCheckBoxList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, IEnumerable<string> selectedValues)
        {
            return ParameterCheckBoxList(xHelper, name, parameterTypeCodeID, selectedValues, "");
        }

        /// <summary>
        /// 普通参数多选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name"></param>
        /// <param name="parameterTypeCodeID"></param>
        /// <param name="selectedValues"></param>
        /// <param name="searchCondition"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterCheckBoxList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, IEnumerable<string> selectedValues, string searchCondition)
        {
            return ParameterCheckBoxList(xHelper, name, parameterTypeCodeID, selectedValues, searchCondition, null);
        }

        /// <summary>
        /// 普通参数多选框列表控件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="name">控件名称</param>
        /// <param name="parameterTypeCodeID">参数类型代码</param>
        /// <param name="selectedValues">选中值</param>
        /// <param name="searchCondition">过滤条件，如：ParameterCodeID > 9</param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ParameterCheckBoxList(this XHtmlHelper xHelper, string name, int parameterTypeCodeID, IEnumerable<string> selectedValues, string searchCondition, object htmlAttributes)
        {
            List<SelectListItem> param = new List<SelectListItem>();

            //条件
            string resultCondition = string.Format("ParameterTypeCodeID = {0}", parameterTypeCodeID);
            if (!string.IsNullOrEmpty(searchCondition))
            {
                resultCondition = string.Format("{0} AND ({1})", resultCondition, searchCondition);
            }

            using (IDataReader dr = ParameterAppI.GetParameterDataReader(resultCondition))
            {
                while (dr.Read())
                {
                    param.Add(new SelectListItem() { Text = SysFunction.StringParse(dr["ParameterName"]), Value = SysFunction.StringParse(dr["ParameterCodeID"]), Selected = (selectedValues != null && selectedValues.Contains(SysFunction.StringParse(dr["ParameterCodeID"]))) });
                }
            }

            return xHelper.Html.CheckBoxList(name, param, htmlAttributes);
        }
        #endregion
    }
}
